<!DOCTYPE html>
<html lang="zh">

<head>

    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>页面</title>
    <script src="https://pinzafile.oss-cn-shanghai.aliyuncs.com/vue-element/loading.js"></script>
    <link href="https://pinzafile.oss-cn-shanghai.aliyuncs.com/vue-element/lib/theme-chalk/index.css" rel="stylesheet">
</head>

<body>
<div id="app">
    <el-container v-loading="loading">
        <el-header>
            <el-breadcrumb separator-class="el-icon-arrow-right">
                <el-breadcrumb-item :to="{ path: '/' }">合作商管理</el-breadcrumb-item>
                <el-breadcrumb-item>合同列表</el-breadcrumb-item>
                <el-breadcrumb-item>合同新增</el-breadcrumb-item>
            </el-breadcrumb>
        </el-header>
        <el-main>
            <el-form ref="contractForm" :model="contractForm" :rules="contractRule" label-width="20%">
                <el-row>
                    <el-col>
                        <el-steps :active="1" simple>
                            <el-step title="合同信息" icon="el-icon-info" style="margin-left:-8%"></el-step>
                        </el-steps>
                    </el-col>
                </el-row>
                <br>
                <el-row>
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
                        <el-form-item label="合同编号" prop>
                            <el-input :disabled="true" placeholder="自动生成"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
                        <el-form-item label="合同名称" prop="contract_name">
                            <el-input v-model="contractForm.contract_name"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
                        <el-form-item label="甲方名称" prop="companyA_id">
                            <el-select v-model="contractForm.companyA_id" filterable placeholder="请选择"
                                       style="width: 100%" disabled>
                                <el-option v-for="item in options" :key="item.id" :label="item.company_name"
                                           :value="item.id">
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>

                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
                        <el-form-item label="乙方名称" prop="companyB_id">
                            <el-select v-model="contractForm.companyB_id" filterable placeholder="请选择"
                                       style="width: 100%" @change="pickRoleName">
                                <el-option v-for="item in options" :disabled="item.id == '1'" :key="item.id"
                                           :label="item.company_name" :value="item.id">
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>

                <el-row>
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
                        <el-form-item label="预计合同金额（万元）" prop="contract_price">
                            <el-input v-model="contractForm.contract_price"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
                        <el-form-item label="有效时间" prop="active_time">
                            <el-date-picker type="date" value-format="yyyy-MM-dd" v-model="contractForm.active_time"
                                            placeholder="选择日期" style="width:100%" @change="changeActiveTime">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
                        <el-form-item label="结束时间" prop="end_time">
                            <el-date-picker type="date" value-format="yyyy-MM-dd" v-model="contractForm.end_time"
                                            placeholder="选择日期" style="width:100%" @change="changeEndTime">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
                        <el-form-item label="付款说明" prop="payment">
                            <el-input type="textarea" v-model="contractForm.payment"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
                        <el-form-item label="合同说明" prop="contract_note">
                            <el-input type="textarea" v-model="contractForm.contract_note"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col>
                        <el-steps :active="1" simple>
                            <el-step title="SKU清单" icon="el-icon-info" style="margin-left:-8%"></el-step>
                        </el-steps>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :xs="6" :sm="6" :md="4" :lg="2" :xl="2">
                        <el-button type="primary" style="margin-top: 12px;" @click="importTariff">导入价目表<i
                                class="el-icon-upload el-icon--right"></i></el-button>
                    </el-col>
                </el-row>
                <el-table v-loading="tableLoading" :data="tables.slice((page-1)*limit,page*limit)"
                          class="tb-edit" style="width: 100%" highlight-current-row
                          size="mini" tooltip-effect="dark" border height="calc(100vh - 428px)">
                    <el-table-column label="序号"
                                     width="50">
                        <template scope="scope"><span>{{scope.$index+(page - 1) * limit + 1}}</span>
                        </template>
                    </el-table-column>
                    <el-table-column label="材料编码" prop="sku_code" :show-overflow-tooltip=true>
                    </el-table-column>
                    <el-table-column label="材料名称" prop="sku_name" :show-overflow-tooltip=true>
                    </el-table-column>
                    <el-table-column label="品牌" prop="sku_brand" :show-overflow-tooltip=true>
                    </el-table-column>
                    <el-table-column label="单位" prop="sku_unit" :show-overflow-tooltip=true>
                    </el-table-column>
                    <el-table-column label="规格参数" prop="sku_slug_set" :show-overflow-tooltip=true>
                    </el-table-column>
                    <el-table-column label="型号" prop="sku_model" :show-overflow-tooltip=true>
                    </el-table-column>
                    <el-table-column label="销售价格" prop="sku_contract_price" :show-overflow-tooltip=true>
                    </el-table-column>
                </el-table>
                <el-row>
                    <el-col>
                        <el-pagination @size-change="SizeChange" @current-change="CurrentChange"
                                       :page-sizes="[100, 500, 1000]"
                                       :page-size="limit" layout="total, sizes, prev, pager, next, jumper"
                                       :total="tables.length">
                        </el-pagination>
                    </el-col>
                </el-row>
                <el-form-item>
                    <el-button type="primary" @click="submitForm">立即创建</el-button>
                    <a href="/contract/list.page">
                        <el-button>返回</el-button>
                    </a>
                </el-form-item>
            </el-form>
        </el-main>
        <el-dialog title="请选择价目表" :visible.sync="dialogFormVisible" width="90%" :fullscreen="true">
            <el-input v-model="dialogTable.keySearch" placeholder="请输入搜索内容" @keyup.enter.native="enterSearch">
                <el-button slot="append" icon="el-icon-search" @click=search></el-button>
            </el-input>
            <el-table v-loading="dialogLoading" ref="multipleTable" :data="dialogTable.tableData" tooltip-effect="dark"
                      style="width: 100%" highlight-current-row
                      @current-change="handleCurrentChange"
                      @select="selectRow"
                      size="mini">
                <el-table-column type="selection" width="55">
                </el-table-column>
                <el-table-column label="序号"
                                 type="index"
                                 width="50">
                </el-table-column>
                <el-table-column prop="code" label="价目表编码" :show-overflow-tooltip=true>
                </el-table-column>
                <el-table-column prop="name" label="价目表名称" :show-overflow-tooltip=true>
                </el-table-column>
            </el-table>
            <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentPage"
                           :page-sizes="[100, 500, 1000]"
                           :page-size="dialogTable.limit" layout="total, sizes, prev, pager, next, jumper"
                           :total="dialogTable.total">
            </el-pagination>
            <el-row>
                <el-button type="primary" @click="checkOK">确定</el-button>
                <el-button @click="checkCancel">取消</el-button>
            </el-row>
        </el-dialog>
    </el-container>
</div>
<script src="https://pinzafile.oss-cn-shanghai.aliyuncs.com/vue-element/dist/vue.js"></script>
<script src="https://pinzafile.oss-cn-shanghai.aliyuncs.com/vue-element/lib/index.js"></script>
<script src="https://pinzafile.oss-cn-shanghai.aliyuncs.com/publiccss/assets/global/plugins/jquery.min.js"
        type="text/javascript"></script>

<script>
    new Vue({
        el: '#app',
        data() {
            return {
                tableLoading: false,
                dialogLoading: false,
                loading: false,
                dialogFormVisible: false,
                contractRule: {
                    contract_name: [{
                        required: true,
                        message: '请输入合同名称',
                        trigger: 'blur'
                    }],
                    companyB_id: [{
                        required: true,
                        message: '请选择乙方名称',
                        trigger: 'change'
                    }],
                    contract_price: [{
                        required: true,
                        message: '请输入预计合同金额（万元）',
                        trigger: 'change'
                    }],
                    contract_type: [{
                        required: true,
                        message: '请选择合作方式',
                        trigger: 'change'
                    }],
                    active_time: [{
                        required: true,
                        message: '请选择起始时间',
                        trigger: 'change'
                    }],
                    end_time: [{
                        required: true,
                        message: '请选择结束时间',
                        trigger: 'change'
                    }],
                },
                contractForm: {
                    active_time: '',
                    companyB_id: "",
                    companyA_id: 1,
                    contract_name: "",
                    contract_price: "",
                    contract_note: "",
                    end_time: "",
                    payment: "",
                    tablePrice_id: "",
                    partyB:"",
                    partyA:'上海品宅装饰科技有限公司',
                },
                options: [],
                tableData: [],
                page: 1,
                limit: 100,
                total: 0,
                keySearch: '',
                multipleSelection: {},
                dialogTable: {
                    tableData: [],
                    limit: 100,
                    page: 0,
                    total: 0,
                },
            }
        },
        computed: {
            tables() {
                var search = this.keySearch;
                if (search) {
                    return this.tableData.filter(data => {
                        return Object.keys(data).some(key => {
                            return String(data[key]).toLowerCase().indexOf(search) > -1;
                        })
                    })
                }
                return this.tableData;
            }
        },
        methods: {
            changeActiveTime(){
                if(this.contractForm.end_time != ''){
                    if(this.contractForm.active_time>this.contractForm.end_time){
                        this.$message.error('有效时间不能大于结束时间');
                        this.contractForm.active_time='';
                    }
                }
            },
            changeEndTime(){
                if(this.contractForm.active_time != ''){
                    if(this.contractForm.active_time>this.contractForm.end_time){
                        this.$message.error('有效时间不能大于结束时间');
                        this.contractForm.end_time='';
                    }
                }
            },
            importTariff() {
                this.dialogFormVisible = true;
                this.getTariffList();
            },
            //回车监听事件获取输入内容
            enterSearch(event) {
                this.dialogTable.page = 1;
                this.dialogTable.limit = 100;
                this.dialogTable.keySearch = event.target.value;
                this.ajaxFunction();
            },
            //点击事件获取输入内容
            search() {
                this.dialogTable.page = 1;
                this.dialogTable.limit = 100;
                this.getTariffList();
            },
            handleCurrentPage(val) {
                this.dialogTable.page = val;
                this.getTariffList();
            },
            handleSizeChange(val) {
                this.dialogTable.limit = val;
                this.getTariffList();
            },
            getTariffList() {
                this.dialogLoading = true;
                $.ajax({
                    url: '/tariffController/skuPriceTableSummaryList.json',
                    type: 'get',
                    data: {key: this.dialogTable.keySearch, page: this.dialogTable.page, limit: this.dialogTable.limit,stat:"00"},
                    dataType: 'json',
                    success: (result) => {
                        console.log(result)
                        if (result.ret) {
                            this.dialogLoading = false;
                            this.dialogTable.tableData = result.data;
                            this.dialogTable.total = result.count;
                        }
                    }
                });
            },
            checkOK() {
                this.dialogFormVisible = false;
                this.contractForm.tablePrice_id = this.multipleSelection.id;
                this.getSkuList();
            },
            SizeChange(val) {
                this.limit = val;
            },
            CurrentChange(val) {
                this.page = val;
            },
            getSkuList() {
                this.tableLoading = true;
                $.ajax({
                    url: '/tariffController/skuPriceTableDetailList.json',
                    type: 'get',
                    data: {
                        id: this.multipleSelection.id,
                        stat: "00",
                        page: 0,
                        limit: 0,
                    },
                    dataType: 'json',
                    success: (result) => {
                        this.tableLoading = false;
                        if (result.ret) {
                            this.tableData = result.data;
                            this.total = result.count;
                        } else {
                            this.$message.error('请求失败，请稍后重试');
                        }
                    }
                });
            },
            checkCancel() {
                this.dialogFormVisible = false;
                this.multipleSelection = {};
            },
            selectRow(selection, row) {
                this.toggleSelection();
                this.toggleSelection(row);
            },
            handleCurrentChange(row) {
                this.toggleSelection();
                this.toggleSelection(row);
            },
            toggleSelection(rows) {
                if (rows) {
                    this.$refs.multipleTable.toggleRowSelection(rows);
                    this.multipleSelection = rows;
                } else {
                    this.$refs.multipleTable.clearSelection();
                }
            },
            pickRoleName(evt) {
                var company = {};
                company = this.options.find((item) => {
                    return item.id === evt;
                });
                this.contractForm.partyB = company.company_name;
            },
            submitForm() {
                if (this.contractForm.tablePrice_id == '') {
                    this.$message.error("请选择价目表");
                    return false;
                }
                if (this.contractForm.contract_name == '') {
                    this.$message.error("请输入合同名称");
                    return false;
                }
                if (this.contractForm.active_time == '') {
                    this.$message.error("请选择生效时间");
                    return false;
                }
                if (this.contractForm.companyB_id == '') {
                    this.$message.error("请选择乙方名称");
                    return false;
                }
                if (this.contractForm.contract_price == '') {
                    this.$message.error("请输入预计合同金额（万元）");
                    return false;
                }
                if (this.contractForm.end_time == '') {
                    this.$message.error("请选择结束时间");
                    return false;
                }
                if(this.contractForm.active_time>this.contractForm.end_time){
                    this.$message.error('有效时间不能大于结束时间');
                    return false;
                }
                if(this.contractForm.companyB_id == '${JDCOMPANY}' && this.contractForm.tablePrice_id != '1'){
                    this.$message.error('上海集栋装饰工程有限公司无法关联其他价目表');
                    return false;
                }
                this.loading = true;
                $.ajax({
                    url: '/contract/addContractSummary.json',
                    type: 'post',
                    data: this.contractForm,
                    dataType: 'json',
                    success: (result) => {
                        console.log(result);
                        this.loading = false;
                        if (result.ret) {
                            this.$message({
                                message: '操作成功',
                                type: 'success'
                            });
                            setTimeout(function () {
                                window.location.href = "/contract/list.page";
                            }, 1000);
                        } else {
                            this.$message.error("创建失败，请稍后重试");
                        }
                    }
                });

            },
            //初始化得的企业列表
            ajaxFunction() {
                $.ajax({
                    url: '/company/select.json',
                    type: 'get',
                    dataType: 'json',
                    success: (result) => {
                        if (result.ret) {
                            this.options = result.data;
                        } else {
                            this.$message.error('获取列表失败,请稍后重试');
                        }
                    }
                });
            },
        },
        mounted() {
            this.ajaxFunction();
        },
    })
</script>

</body>

</html>